<template>
	<view class="confirm-content">
		<view class="order-box">
			<view class="course-box">
				<view class="course-img">
					<image :src="productInfo.full_cover" mode="aspectFill"></image>
				</view>
				<view class="course-detail">
					<view>{{productInfo.title}}</view>
					<view>￥{{productInfo.price}}</view>
				</view>
			</view>
			<view class="price-box">
				<view>总价</view>
				<view>￥{{productInfo.price}}</view>
			</view>
			<!-- <view class="concat-box">
				<view class="concat-title">联系方式（选填）</view>
				<view class="input-box">
					<u-input v-model="value" type="number" placeholder='输入手机号码' />
					<u-button :custom-style="customStyle" open-type='getPhoneNumber'
						@getphonenumber="decryptPhoneNumber">一键获取</u-button>
				</view>
			</view> -->
			<!-- <view class="pay-box">
								<view>请选择支付方式</view>
				<view class="check-box">
					<view class="select-item" v-for="(item,index) in payTypeList" :key="item.id" @click="selectPay(item)">
						<view class="select-title">
							<view class="title-image">
								<image :src="item.payImage" mode=""></image>
							</view>
							<view>{{item.payName}}</view>
						</view>
						<view class="select-text">
							<image src="../../static/index/selected.png" mode="" v-if="item.checked"></image>
							<image src="../../static/index/select.png" mode="" v-if="!item.checked"></image>
						</view>
					</view>
				</view>
			</view> -->
			<view class="pay-know">
				<view class="neron">
					<view style="flex:none;">购买须知</view>
					<image src="../../static/index/right.png" mode="aspectFill"></image>
				</view>
				<view>
					{{productInfo.purchase_instructions?productInfo.purchase_instructions:''}}
				</view>
			</view>
		</view>

		<view class="sub-btn">
			<zijie-pay-button :goodsId="productInfo.id"></zijie-pay-button>
			<!-- <u-button type="primary" shape="circle" :custom-style="customStyle1" @click="pay">立即支付</u-button> -->
		</view>
		<u-popup v-model="paySuccess" mode="center" border-radius="20" width="600rpx" height="400rpx">
			<view class="popup-box">
				<view class="popup-title">
					<image src="../../static/index/selected.png" mode=""></image>
					<view>支付成功</view>
				</view>
				<view>
					课程已购买，赶快去学习起来
				</view>
				<view>
					<u-button type="primary" shape="circle" :custom-style="customStyle1" @click="isClose">知道了</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		orderPayDY
	} from '../../api/pay.js';
	import {
		toast
	} from '../../utils/common.js';
	import {
		wxpay,
		alipay
	} from '@/utils/pay.js'
	export default {
		components: {
			// #ifdef  MP-TOUTIAO
			"zijie-pay-button": "../../ttcomponents/DyPayButton/index",
			// #endif
		},
		data() {
			return {
				paySuccess: false,
				customStyle: {
					border: 'none',
					color: '#2AC092',
					background: 'transparent'
				},
				customStyle1: {
					color: '#fff',
					background: '#2AC092'
				},
				payTypeList: [{
					id: 1,
					payName: '抖音支付',
					payImage: '../../static/index/wechat.png',
					checked: true
				}],
				orderNo: '',
				payType: '抖音支付',
				productInfo: {}
			}
		},
		onLoad(options) {
			this.orderNo = options.order_no;
			let obj = JSON.parse(decodeURIComponent(options.productInfo));
			this.productInfo = obj;
			console.log("数据", options, obj);
			console.log(tt.canIUse("industrySDK.pay-button"));

		},
		methods: {

			decryptPhoneNumber(e) {
				console.log(e);
			},
			isClose() {
				uni.navigateBack()
			},
			selectPay(item) {
				this.payTypeList.forEach(ele => {
					ele.checked = false
				})
				item.checked = true;
				this.payType = item.payName
			},
			handleContinutePay(event) {
				console.log(event);
				const {
					status,
					outOrderNo,
					result
				} = event.detail;
				if (status === 'success') {
					const {
						code
					} = result;
					if (code === 0) {
						// 继续支付成功
					}
				} else {
					// 继续支付失败
				}
			},
			pay() {
				let that = this
				console.log("订单号", this.orderNo);
				orderPayDY({
					order_no: this.orderNo
				}).then(res => {
					console.log(888888, res);
					if (res.data.code !== 200) {
						toast(res.data.message)
					} else {
						console.log("调用支付接口");
						tt.createOrder({
							goodsList: [{
								quantity: 1, // 购买数量 必填
								price: Number(this.productInfo.price), // 商品价格 必填

								goodsName: this.productInfo.title, // 商品名称 必填
								goodsPhoto: this.productInfo.full_cover, // 商品图片链接 必填
								goodsId: String(this.productInfo.id), // 商品ID 必填
								goodsType: 1, // 商品类型 必填
							}, ],
							payment: {
								totalAmount: Number(this.productInfo.price), // 订单总价 必填
							},
							success: (res) => {
								const {
									orderId,
									outOrderNo
								} = res;
								console.log("success res", res);
								console.log("orderId", orderId, "outOrderNo", outOrderNo);
								this.setData({
									orderId,
									outOrderNo
								});
							},
							fail: (res) => {
								const {
									orderId,
									outOrderNo,
									errNo,
									errMsg,
									errLogId
								} = res;
								if (errLogId) {
									console.log("预下单失败", errNo, errMsg, errLogId);
								}
								if (orderId || outOrderNo) {
									console.log("支付失败", errNo, errMsg, orderId, outOrderNo);
								}
								console.log("报错", errNo, errMsg);
							},
						});


						// uni.requestPayment({
						// 	provider: 'wxpay',
						// 	orderInfo:res.data.data,
						// 	service:5,
						// 	getOrderStatus: function(resp) {
						// 		console.log(55555,resp);
						// 	},
						// 	success: function(res) {
						// 		// success && success(res.data);
						// 		if(res.code == 0){
						// 			that.paySuccess = true
						// 			toast('支付成功')
						// 		}
						// 		if(res.code == 1){
						// 			toast('支付超时')
						// 		}
						// 		if(res.code == 2){
						// 			toast('支付失败')
						// 		}
						// 		if(res.code == 3){
						// 			toast('支付关闭')
						// 		}
						// 		if(res.code == 4){
						// 			toast('支付取消')
						// 		}
						// 	},
						// 	fail: function(err) {
						// 		uni.showToast({
						// 			title: '支付失败',
						// 			icon: 'none',
						// 			duration: 2000
						// 		});
						// 		// fail && fail(err.data);
						// 	},
						// 	complete(ress) {
						// 		console.log(ress);
						// 		// complete && complete(ress.data);
						// 	}
						// });
					}

				}).catch()
				// if(this.payType == '支付宝支付'){
				// 	orderPayDY({order_no:this.orderNo}).then(res=>{
				// 		if(res.data.code!== 200){
				// 			toast(res.data.message)
				// 		}else{
				// 			uni.requestPayment({
				// 				provider: 'alipay',
				// 				orderInfo:res.data.data,
				// 				service:5,
				// 				getOrderStatus: function(resp) {
				// 					console.log(resp);
				// 				},
				// 				success: function(res) {
				// 					// success && success(res.data);
				// 					that.paySuccess = true
				// 					toast('支付成功')
				// 				},
				// 				fail: function(err) {
				// 					uni.showToast({
				// 						title: '支付失败',
				// 						icon: 'none',
				// 						duration: 2000
				// 					});
				// 					// fail && fail(err.data);
				// 				},
				// 				complete(ress) {
				// 					console.log(ress);
				// 					// complete && complete(ress.data);
				// 				}
				// 			});
				// 		}

				// 	}).catch()
				// }
				// this.paySuccess = true;
				// orderPayDY().then().catch()
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-hairline-border::after {
		border: none !important;
	}

	.order-box {
		padding: 20rpx;

		.course-box {
			background-color: #fff;
			border-radius: 10rpx;
			padding: 20rpx;
			display: flex;

			.course-img {
				width: 240rpx;
				height: 160rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;

				>image {
					border-radius: 10rpx;
					width: 100%;
					height: 100%;
				}
			}

			.course-detail {
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				>view:nth-child(1) {
					font-size: 30rpx;
					line-height: 1.5;
				}

				>view:nth-child(2) {
					font-size: 36rpx;
					color: #FF6D6A;
				}
			}
		}

		.price-box {
			display: flex;
			justify-content: space-between;
			padding: 20rpx;
			background-color: #fff;
			border-radius: 10rpx;
			margin-top: 20rpx;
		}

		.concat-box {
			padding: 20rpx;
			background-color: #fff;
			margin-top: 20rpx;
			border-radius: 10rpx;
			font-size: 30rpx;

			.input-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 20rpx;
			}
		}

		.pay-box {
			padding: 20rpx;
			background-color: #fff;
			margin-top: 20rpx;
			border-radius: 10rpx;
			font-size: 30rpx;

			.check-box {
				margin-top: 20rpx;

				.select-item {
					padding: 20rpx;
					display: flex;
					justify-content: space-between;

					.select-title {
						display: flex;
						align-items: center;

						.title-image {
							margin-right: 28rpx;
							width: 50rpx;
							height: 50rpx;

							>image {
								width: 100%;
								height: 100%;
							}
						}
					}

					.select-text {
						width: 36rpx;
						height: 36rpx;

						>image {
							width: 100%;
							height: 100%;
						}
					}

				}
			}
		}

		.pay-know {
			padding: 20rpx;
			background-color: #fff;
			margin-top: 20rpx;
			border-radius: 10rpx;

			.neron {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;

				>image {
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
	}

	.sub-btn {
		position: fixed;
		bottom: 10rpx;
		padding: 20rpx;
		width: 100%;
		background-color: #fff;
	}

	.popup-box {
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		height: 100%;

		.popup-title {
			display: flex;
			flex-direction: column;
			align-items: center;

			>image {
				width: 72rpx;
				height: 72rpx;
				margin-bottom: 20rpx;
			}

			>view {
				color: #2AC092;
				font-size: 30rpx;
			}
		}
	}
</style>